/**
 * Created by Administrator on 2018/7/6 0006.
 */
$.validator.addMethod("phone", function(value, element) {
    var length = value.length;
    var mobile =  /^(((1[0-9]{1})|(1[0-9]{1}))+\d{9})$/;
    return this.optional(element) || (length == 11 && mobile.test(value));
}, "手机号码格式错误");

// 电话号码验证
$.validator.addMethod("tel", function(value, element) {
    var tel = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
    return this.optional(element) || (tel.test(value));
}, "电话号码格式错误");

//电话号码验证
$.validator.addMethod("positiveInt", function(value, element) {
    var tel = /^[0-9]*[1-9][0-9]*$/;
    return this.optional(element) || (tel.test(value));
}, "只能输入正整数.");

$.validator.addMethod("ip", function(value, element) {
    var ip = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
    return this.optional(element) || (ip.test(value) && (RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256));
}, "Ip地址格式错误");

// 菜单排序号验证
$.validator.addMethod("ordinal", function(value, element) {
    var ordinal = /^\d*[1-9]\d*$/;
    return this.optional(element) || (ordinal.test(value));
}, "菜单排序号格式错误");


// 角色权限绑定验证
$.validator.addMethod("saveRole", function(value, element) {
    var selectPermissions = tree.getAllChecked();
    //alert("selectPermissions"+selectPermissions);
    if(!selectPermissions) {
        //alert("请为角色["+roleName+"]绑定权限！");
        return false;
    }
    var pArray = selectPermissions.split(",");
    for( i = 0; i < pArray.length; i++) {
        var eachP = pArray[i];
        var eachParent = tree.getParentId(eachP);

        while(eachParent != "0" && eachParent != "") {
            if($.inArray(eachParent, pArray) < 0) {
                pArray.push(eachParent);
            }
            eachParent = tree.getParentId(eachParent);
        }
    }
    $('#permissions').val(pArray.join());
    return true;
}, "请为角色绑定权限");

//确认新密码
$.validator.addMethod("pwdsame", function(value, element) {
    return $('#newpassword').val()==$('#surepassword').val();
}, "两次输入的新密码不相同");

//确认新密码
$.validator.addMethod("pwdsame2", function(value, element) {
    return $('#password').val()==$('#confirmpassword').val();
}, "两次输入的新密码不相同");

//手机号码验证
$.validator.addMethod("userPhone", function(value, element) {
    var length = value.length;
    var mobile =  /^(((1[0-9]{1})|(1[0-9]{1}))+\d{9})$/;
    var userIdentityCode = $("#userIdentityCode").val();
    if (userIdentityCode == 'UF002'){
        var vArray = value.split(',');
        for (var i = 0;i<vArray.length;i++){
            if (vArray[i].length != 11 || !mobile.test(vArray[i])){
                return false;
            }
        }
        return true;
    }else{
        if (length != 11){
            return false;
        }
    }
    return this.optional(element) || mobile.test(value);
}, "手机号码格式错误");

//手机号码验证
$.validator.addMethod("userNameFormat", function(value, element) {
    var length = value.length;
    var format =  /^[a-zA-z][a-zA-Z0-9_]{1,19}$/;
    return this.optional(element) || format.test(value);
}, "用户名格式错误");
//手机号码验证
$.validator.addMethod("idcard", function(value, element) {
    var length = value.length;
    var format = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;
    return this.optional(element) || format.test(value);
}, "身份证格式错误");

$.validator.addMethod("classCodeValidate", function(value, element) {
    var controls = $("#gradeClassControl > .controls");
    var controlsMap = {},repeat=true;
    controls.each(function(i,o){
        var gradeCode = $(this).find("select[name=gradeCode]").val();
        var classCode = $(this).find("select[name=classCode]").val();
        if (!gradeCode || !classCode){
            repeat=false;
        }
        if (controlsMap[gradeCode] && controlsMap[gradeCode] == classCode){
            repeat=false;
        }else{
            controlsMap[gradeCode] = classCode;
        }
    });
    return repeat;
}, "年级班级不能为空或重复。");

$.validator.addMethod("enrollendTime",
    function(value, element) {
        var startDate = $('#enrollstartTime').val();
        return new Date(Date.parse(startDate.replace("-", "/"))) <= new Date(Date.parse(value.replace("-", "/")));
    },
    "结束日期必须大于开始日期!");

$.validator.addMethod("examendTime",
    function(value, element) {
        var startDate = $('#examstartTime').val();
        return new Date(Date.parse(startDate.replace("-", "/"))) <= new Date(Date.parse(value.replace("-", "/")));
    },
    "结束日期必须大于开始日期!");

$.validator.addMethod("regexPassword", function(value, element) {
    return this.optional(element) || /^(?=^.{6,18}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/.test(value);
}, "一个大写，一个小写，一个符号");

$.validator.addMethod("productURL", function(value, element) {
    var link = value;
    var plat = parseInt($("#taskDetail").val());
    switch (plat) {
        case 1: //淘宝平台
            return /^(http|https):\/\/.*\.(taobao|tmall)\..+\//.test(link)
                || /^(http|https):\/\/detail\.yao/.test(link);
        case 2: //淘宝平台
            return /^(http|https):\/\/.*\.(taobao|tmall)\..+\//.test(link)
                || /^(http|https):\/\/detail\.yao/.test(link);
        case 5: //淘宝平台
            return /^(http|https):\/\/.*\.(taobao|tmall)\..+\//.test(link)
                || /^(http|https):\/\/detail\.yao/.test(link);
        case 6: //美丽说
            return /^(http|https):\/\/.*\.meilishuo\.com\//.test(link);
        case 7: //京东
            return /^(http|https):\/\/.*\.jd\.com\//.test(link);
        case 8: //蘑菇街
            return /^(http|https):\/\/.*\.mogujie\.com\//.test(link);
        default:
            return false;
    }
}, "请输入正确的商品链接");
